package com.neutech.img.tank;

import com.neutech.Util.ImageUtils;
import com.neutech.base.Drawable;
import com.neutech.base.Sprite;
import com.neutech.constant.Constant;
import com.neutech.frame.TankFrame;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class Home extends Sprite implements Drawable {

   private transient Map<String,Image> imageMap ;
//    private TankFrame tankFrame; //TODO 声明的
//    public Home(TankFrame tankFrame){  //TODO 构造函数
//        this.tankFrame = tankFrame;
//    }
/*反序列化不支持构造函数,会让inagemap无法new，只能为空下draw方法里的是解决办法*/
    /**
     * 记录是那张图
     */
    private boolean live = true;

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public Home() {
        this(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE*6,
                Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12,
                Constant.SPRITE_SIZE,Constant.SPRITE_SIZE);

    }

    public Home(int x, int y, int width, int height) {
        super(x, y, width, height);
//        System.out.println(123);
    imageMap = new HashMap<>();//先newmap再去loadingimg去
    loadingImg();
    }
//    public void die(){
//        if (!live){
//            tankFrame.changeStatus(5);//TODO 老家死
//        }
//    }



    @Override
    public void draw(Graphics g) {
        /*解决反序列化带来的问题*/
        if (imageMap == null){
            imageMap = new HashMap<>();
            loadingImg();
        }/*解决反序列化带来的问题*/
        if (live){
            g.drawImage(imageMap.get("live"),getX(),getY(),getWidth(),getHeight(),null);
        }else {
            g.drawImage(imageMap.get("die"),getX(),getY(),getWidth(),getHeight(),null);

        }

    }

    @Override
    public Rectangle getRectangle() {
        return new Rectangle(getX(),getY(),getWidth(),getHeight());
    }
    public void loadingImg(){
//        System.out.println(123);
        imageMap.put("live", ImageUtils.getValue("home_live"));
        imageMap.put("die", ImageUtils.getValue("home_die"));

    }


}
